//--------------------------------------------------------------
//
// Qizhi Zhang 
// ezhung@gmail.com
// http://www.moorwind.com
//
//--------------------------------------------------------------
package reader.utils
{
	import com.hurlant.crypto.Crypto;
	import com.hurlant.crypto.symmetric.ICipher;
	import com.hurlant.crypto.symmetric.IPad;
	import com.hurlant.crypto.symmetric.PKCS5;
	import com.hurlant.util.Base64;
	import com.hurlant.util.Hex;
	
	import flash.utils.ByteArray;
	
	public class CryptoUtil
	{
		public static function encrypt(text:String, k:String):String
		{
			var kdata:ByteArray = Hex.toArray(k);
			var data:ByteArray =  Hex.toArray(Hex.fromString(text));
			var pad:IPad = new PKCS5();
			var mode:ICipher = Crypto.getCipher("simple-xtea-ecb", kdata, pad);
			pad.setBlockSize(mode.getBlockSize());
			mode.encrypt(data);
			return Base64.encodeByteArray(data);
		}
		
		public static function decrypt(text:String, k:String):String
		{
			var kdata:ByteArray = Hex.toArray(k);
			var data:ByteArray = Base64.decodeToByteArray(text);
			var pad:IPad = new PKCS5();
			var mode:ICipher = Crypto.getCipher("simple-xtea-ecb", kdata, pad);
			pad.setBlockSize(mode.getBlockSize());
			mode.decrypt(data);
			return Hex.toString(Hex.fromArray(data));
		}

	}
}